﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using ProjManagSolution.pmMyWebService;

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Data;

namespace ProjManagSolution
{
    public partial class MainPage : Page
    {
        List<Project> projectListRecent;
        List<Project> projectList;

        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        protected void showRecentProjectList(List<project> input)
        {
            projectListRecent = new List<Project>();

            // Generate projectList
            int itemsCount = input.Count;
            for (int i = 0; i < itemsCount; i++)
            {
                string tido = "";
                if (input[i].user != null)
                {
                    
                    if (input[i].user.user_firstname == "N/A" && input[i].user.user_lastname == "N/A")
                        tido = "N/A";
                    else if (input[i].user.user_firstname == "N/A" && input[i].user.user_lastname != "N/A")
                        tido = input[i].user.user_firstname;
                    else if (input[i].user.user_firstname != "N/A" && input[i].user.user_lastname == "N/A")
                        tido = input[i].user.user_lastname;
                    else
                        tido = input[i].user.user_firstname + " " + input[i].user.user_lastname;
                }
                projectListRecent.Add(new Project()
                {
                    ID = input[i].project_id.ToString(),
                    Name = input[i].project_name,
                    StartDate = input[i].project_start_date,
                    Manager = tido,
                    Description = input[i].project_description
                });
                
            }
            this.datagridRecentProject.ItemsSource = projectListRecent;
        }

        protected void showProjectList(List<project> input)
        {
            projectList = new List<Project>();

            // Generate projectList
            int itemsCount = input.Count;
            for (int i = 0; i < itemsCount; i++)
            {
                if (input[i].user != null)
                {
                    string tido;
                    if (input[i].user.user_firstname == "N/A" && input[i].user.user_lastname == "N/A")
                        tido = "N/A";
                    else if (input[i].user.user_firstname == "N/A" && input[i].user.user_lastname != "N/A")
                        tido = input[i].user.user_firstname;
                    else if (input[i].user.user_firstname != "N/A" && input[i].user.user_lastname == "N/A")
                        tido = input[i].user.user_lastname;
                    else
                        tido = input[i].user.user_firstname + " " + input[i].user.user_lastname;

                    projectList.Add(new Project()
                    {
                        ID = input[i].project_id.ToString(),
                        Name = input[i].project_name,
                        StartDate = input[i].project_start_date,
                        Manager = tido,
                        Description = input[i].project_description
                    });
                }
            }
            this.datagridProjectList.ItemsSource = projectList;
            this.txtBlokPortfolio.Text = "We currently have " + itemsCount.ToString() + " project(s) participated.";
        }

        protected void showUserList(List<user> input)
        {
            List<User> userList = new List<User>();
            string[] sexValue = new string[2] { "Female", "Male" };
            // Generate userList
            int itemsCount = input.Count;
            for (int i = 0; i < itemsCount; i++)
            {
                if ((int)input[i].user_sex == -1)
                    userList.Add(new User(input[i].user_id.ToString(), input[i].user_email,
                    input[i].user_firstname,
                    input[i].user_lastname,
                    ((DateTime)input[i].user_birthday),
                    "N/A",
                    input[i].user_address,
                    input[i].user_phone
                ));
                else
                    userList.Add(new User(input[i].user_id.ToString(), input[i].user_email,
                        input[i].user_firstname,
                        input[i].user_lastname,
                        ((DateTime)input[i].user_birthday),
                        sexValue[(int)input[i].user_sex],
                        input[i].user_address,
                        input[i].user_phone
                    ));
            }
            this.datagridUserList.ItemsSource = userList;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            pmWebServiceClient ws = new pmWebServiceClient();
            ////get project list
            ws.GetProjectsCompleted += new EventHandler<GetProjectsCompletedEventArgs>(ws_GetProjectsCompleted);
            ws.GetProjectsAsync();

            //get user list
            ws.GetUsersCompleted += new EventHandler<GetUsersCompletedEventArgs>(ws_GetUsersCompleted);
            ws.GetUsersAsync();

        }

        void ws_GetUsersCompleted(object sender, GetUsersCompletedEventArgs e)
        {
            List<user> ul = e.Result;
            showUserList(ul);
        }


        void ws_GetProjectsCompleted(object sender, GetProjectsCompletedEventArgs e)
        {
            List<project> lp = e.Result;
            
            showRecentProjectList(lp);
            showProjectList(lp);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void buttLogin_Click(object sender, RoutedEventArgs e)
        {
            pmWebServiceClient tido = new pmWebServiceClient();
            tido.GetUserDetailByAuthCompleted += new EventHandler<GetUserDetailByAuthCompletedEventArgs>(ws_GetUserDetailByAuthCompleted);
            
            string password = passboxPassword.Password.ToString();
            string username = txtboxEmail.Text.ToString();

            tido.GetUserDetailByAuthAsync(username, password);
        }

        void ws_GetUserDetailByAuthCompleted(object sender, GetUserDetailByAuthCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                //create session
                Session s = new Session();
                s.InsertUserEmail(txtboxEmail.Text.ToString());

                PageSwitch ps = this.Parent as PageSwitch;
                ps.Navigate(new UserControlPanel());
            }
            else
            {
                MessageBox.Show("Email and password do not match!");
            }
        }

        private void buttRegister_Click(object sender, RoutedEventArgs e)
        {
            PageSwitch ps = this.Parent as PageSwitch; 
            ps.Navigate(new Register());
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.Storyboard1.Begin();
        }

        private void buttSearchProject_Click(object sender, RoutedEventArgs e)
        {
            pmWebServiceClient ws = new pmWebServiceClient();
            ws.GetProjectsBySearchStringCompleted += new EventHandler<GetProjectsBySearchStringCompletedEventArgs>(ws_GetProjectsBySearchStringCompleted);
            ws.GetProjectsBySearchStringAsync(txtboxSearchProject.Text.ToString());
        }

        void ws_GetProjectsBySearchStringCompleted(object sender, GetProjectsBySearchStringCompletedEventArgs e)
        {
            List<project> resultl = new List<project>();
            resultl = e.Result;

            showProjectList(resultl);
        }

        private void buttSearchUser_Click(object sender, RoutedEventArgs e)
        {
            pmWebServiceClient ws = new pmWebServiceClient();
            ws.GetUserBySearchStringCompleted += new EventHandler<GetUserBySearchStringCompletedEventArgs>(ws_GetUserBySearchStringCompleted);
            ws.GetUserBySearchStringAsync(txtboxSearchUser.Text.ToString());
        }

        void ws_GetUserBySearchStringCompleted(object sender, GetUserBySearchStringCompletedEventArgs e)
        {
            List<user> userl = new List<user>();
            userl = e.Result;

            showUserList(userl);
        }
    }
}
